package com.clip.job;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.clip.entity.User;
import com.clip.entity.UserEmail;
import com.clip.util.IosMessagePusher;
import com.clip.util.mail.SimpleMailSender;
import com.clip.util.mobile.MobileHttpSender;
import com.clip.util.mobile.MobileMsgPattern;
public class AlertJob implements Job {
	
	public void execute(JobExecutionContext ctx) throws JobExecutionException {
		JobDetail jobDetail=ctx.getJobDetail();
		String jobname=jobDetail.getKey().getName();
		if(jobname.startsWith("clipevent")){
			int index=jobname.indexOf("_");
			int eventid=Integer.parseInt(jobname.substring(index+1));
			JobDataMap map=ctx.getMergedJobDataMap();
			User user=(User) map.get("user");
			List<String> userEmails=(List<String>) map.get("useremails");
			String url=(String) map.get("url");
			String title=(String) map.get("title");
			Map<String, Object> root = new HashMap<String, Object>();
			root.put("username", user.getName());
			root.put("url", url);
			root.put("title", title);
			String mailcontent = com.clip.util.FtlUtils.stringRender(root,"remindevent.ftl");
			for (String userEmail : userEmails) {
				SimpleMailSender.send(mailcontent, user.getName()+"，你有一个来自档期的消息", userEmail);
			}
			int mobilenotice=map.getInt("mobilenotice");
			if(mobilenotice==1){
				String mobilenum=map.getString("mobilenum");
				MobileHttpSender.sendMobileMsg(mobilenum, MobileMsgPattern.getMobileClockMsg(user.getName(), map.getString("title")));
			}
			String dt=(String) map.get("devicetoken");
			if(dt!=null&&dt.length()>0){
				IosMessagePusher.pushClipeventAlert(dt, "档期提醒："+title);
			}
		}
		
	}
	
//	private void getMemoEventStarttime(Calendar clocktime,String clock){
//		if(clock.equals("0")){
//			return;
//		}else if(clock.startsWith("min")){
//			String time=clock.substring(clock.indexOf("n")+1);
//			int beforetime=Integer.parseInt(time);
//			clocktime.add(Calendar.MINUTE,beforetime);
//			
//		}else if(clock.startsWith("hour")){
//			String time=clock.substring(clock.indexOf("r")+1);
//			int beforetime=Integer.parseInt(time);
//			clocktime.add(Calendar.HOUR_OF_DAY,beforetime);
//		}else if(clock.startsWith("day")){
//			String time=clock.substring(clock.indexOf("y")+1);
//			int beforetime=Integer.parseInt(time);
//			clocktime.add(Calendar.DAY_OF_MONTH,beforetime);
//		}else if(clock.startsWith("week")){
//			String time=clock.substring(clock.indexOf("k")+1);
//			int beforetime=Integer.parseInt(time);
//			clocktime.add(Calendar.WEEK_OF_YEAR,beforetime);
//		}else if(clock.startsWith("month")){
//			String time=clock.substring(clock.indexOf("h")+1);
//			int beforetime=Integer.parseInt(time);
//			clocktime.add(Calendar.MONTH,beforetime);
//		}
//	}
//
//	
//	private String getEventType(String eventtype){
//		if(eventtype.equals("other"))
//			return "其他";
//		else if(eventtype.equals("work"))
//			return "工作";
//		else if(eventtype.equals("socal"))
//			return "社交";
//		else if(eventtype.equals("private"))
//			return "私人";
//		else
//			return null;
//	}
}
